package com.hns.can.master.web;

import java.io.File;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.component.ParseStnLgtLttExcel;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.entity.BaseStnInfoEntity;
import com.hns.can.master.service.StationInfoService;
import com.hns.can.master.vo.StnPostDataVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.FileUploadUtil;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.ResponseUtil;
import com.hns.tool.pub.UtilString;

/**
 * 
 * 站点信息维护控制类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/stationInfo")
public class StationInfoController extends BaseController {
    
	/**
	 * 注入stationInfoService
	 */
    @Autowired
    private StationInfoService stationInfoService; 
    
    /**
	 * 导出文件表头
	 */
    private final String[] tableHeaderNames={"站点编号","站点名称","站点别名","方向","经度","纬度","进站半径","出站半径","限速","所在道路","位置信息","备注"};
    /**
	 * 导出文件字段名
	 */
    private final String[] tableHeaderFields = {"stnCd","stnName","stnAlias","direction","lgt","ltt","inRadius","outRadius","speedLimit","road","loDetailInfo","remark"};
    
    /**
     * 添加一个站点
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/addStationInfo")
    @ResponseBody
    public Object addStationInfo(BaseStnInfoEntity entity) throws Exception {
        stationInfoService.addStationInfo(entity, this.getCurrentUser().getUser());
        return this.getReturnMsg("您成功添加一个站点");
    }
    
    /**
     * 得到站点的page对象
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchStationInfoPage")
    @ResponseBody
    public Object searchStationInfoPage(StnPostDataVo dataVo) throws Exception {
        Pagination page = stationInfoService.searchStationInfoPage(dataVo,this.getCurrentUser().getUser());
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 导出站点信息excel
     * @param dataVo
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStationInfoExcel")
    @ResponseBody
    public void exportStationInfoExcel(StnPostDataVo dataVo,HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = stationInfoService.getStationInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "stationInfo", "站点信息");
    }

    /**
     * 站点信息模板
     * @param response
     * @throws Exception
     * @author:chenl
     * @email:370238764@qq.com
     */
    @RequestMapping("/exportStationInfoTemplate")
    @ResponseBody
    public void exportStationInfoTemplate(HttpServletResponse response) throws Exception {
//        List<BaseEntity> list = new ArrayList<BaseEntity>();
//        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "stationInfo", "站点信息");
        String fileName = "stationInfo.xls";
        String targetPath = FileUploadUtil.getWebRoot(request) + "/template/" + fileName;
        File tempFile = new File(targetPath);
        if(!tempFile.exists()){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有模版可以下载！");
        }
        ExportUtil.GeneratoDownFile(targetPath, fileName.substring(0,fileName.lastIndexOf('.')),".xls",response);
    }
    
    /**
     * 导出站点信息pdf
     * @param dataVo
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStationInfoPdf")
    @ResponseBody
    public void exportStationInfoPdf(StnPostDataVo dataVo, HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = stationInfoService.getStationInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "stationInfo", "站点信息");
    }
    
    /**
     * 根据站点id求站点信息
     * @param stnId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getStationInfoByStnId")
    @ResponseBody
    public Object getStationInfoByStnId(@RequestParam(value="stnId") String stnId) throws Exception {
        BaseStnInfoEntity entity = stationInfoService.getStationInfoByStnId(stnId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 更新站点的信息
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/uptStationInfo")
    @ResponseBody
    public Object uptStationInfo(BaseStnInfoEntity entity) throws Exception {
        stationInfoService.uptStationInfo(this.getCurrentUser().getUser(), entity);
        return this.getReturnMsg("保存成功");
    }
    
    /**
     * 删除一个站点
     * @param stnIds
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/delStationInfo")
    @ResponseBody
    public Object delStationInfo(@RequestParam(value="stnIds") String[] stnIds) throws Exception {
        stationInfoService.delStationInfo(this.getCurrentUser().getUser(), stnIds);
        return this.getReturnMsg("站点删除成功");
    }
    
    /**
     * 获取公司下所有启用线路信息
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getBaseLineInfoList")
    @ResponseBody
    public Object getBaseLineInfoList(BaseLineInfoEntity entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        String corpId = user.getCorpId();
        entity.setCorpId(corpId);
        List<BaseLineInfoEntity> list = stationInfoService.getBaseLineInfoList(entity);
        return this.getReturnResult(list);
    }
    
    /**
     * 导出站点经纬度模版
     * @param qLineId
     * @param qLineType
     * @param response
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStatLgtLtt2Excel")
    @ResponseBody
    public Object exportStatLgtLtt2Excel(@RequestParam("qLineId") String qLineId, 
    		@RequestParam("qLineType") String qLineType, HttpServletRequest request,HttpServletResponse response) throws Exception {
        List<BaseEntity> list = new ArrayList<BaseEntity>();
            ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "StatLgtLtt", "站点经纬度");
        return this.getReturnResult();
    }
    
    /**
     * 上传站点经纬度
     * @param statLgtLttFile
     * @param request
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/uploadStatLgtLttFile")
    @ResponseBody
    public Object uploadStatLgtLttFile(
        @RequestParam(value = "statLgtLttFile", required = true) MultipartFile statLgtLttFile,
                HttpServletRequest request,HttpServletResponse response,String corpId) throws Exception {
        // 将excel文件存盘
        String oldFileName = statLgtLttFile.getOriginalFilename();
        String newFileName = UtilString.getUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
        String targetPath = FileUploadUtil.getWebRoot(request) + "/template/temp/";
        FileUploadUtil.uploadFile(statLgtLttFile.getInputStream(), newFileName, targetPath);
        // 解析excel文件得到list对象
        String fileName = targetPath + newFileName; // 完整路径
        File file = new File(fileName);
        List<BaseStnInfoEntity> list = ParseStnLgtLttExcel.parseExcelToList(file);
        // 存入对应的库表
        String msgs = stationInfoService.saveToDB(list, this.getCurrentUser().getUser(),corpId);
        int successCnt = stationInfoService.getSuccessCount();
        // 删除临时文件
        FileUploadUtil.delFile(fileName);
        msgs = ",失败"+(list.size()-successCnt)+"条  <div> "  +  msgs + " </div> " ;
        if(successCnt == list.size()){
            msgs = "";
        }
        ResponseUtil.responseJSON(this.getReturnMsg("总共"+list.size()+"条站点数据,成功导入"+successCnt+"条"+msgs),response);
        return null;
//        return this.getReturnMsg("导入成功");
    }
}
